/*
 * File Name:               XCommonPool.cs
 * 
 * Description:             通用简单缓存池
 * Author:                  lisiyu <576603306@qq.com>

 * Create Date:             2017/04/10
 */

using System;
using System.Collections.Generic;

public class XCommonPool<T> where T : new()
{
    private Stack<T> mObjs;

    public XCommonPool()
    {
        mObjs = new Stack<T>();
    }

    public XCommonPool(int count)
    {
        mObjs = new Stack<T>(count);
    }

    public void push(T instance)
    {
        mObjs.Push(instance);
    }

    public T pop(Func<T> callback)
    {
        if (mObjs.Count == 0)
        {
            return callback();
        }
        else
        {
            return mObjs.Pop();
        }
    }

    public T pop()
    {
        if (mObjs.Count == 0)
        {
            return new T();
        }
        else
        {
            return mObjs.Pop();
        }
    }
}
